package org.zlpt.contract.dao;

import com.owlike.genson.Genson;
import org.hyperledger.fabric.contract.Context;
import org.hyperledger.fabric.contract.ContractInterface;
import org.hyperledger.fabric.contract.annotation.Contract;
import org.hyperledger.fabric.contract.annotation.Transaction;
import org.hyperledger.fabric.shim.ChaincodeException;
import org.hyperledger.fabric.shim.ChaincodeStub;
import org.zlpt.entity.co.PaymentRecordCO;


@Contract(
        name = "paymentRecord"
)
public final class PaymentRecordContract implements ContractInterface {
    private final Genson genson = new Genson();

    @Transaction(intent = Transaction.TYPE.EVALUATE)
    public boolean PaymentRecordExists(final Context ctx, String paymentRecordJson){
        ChaincodeStub stub = ctx.getStub();
        PaymentRecordCO paymentRecord = genson.deserialize(paymentRecordJson, PaymentRecordCO.class);
        String paymentRecordJSON = stub.getStringState(paymentRecord.formKey());

        return (paymentRecordJSON != null && !paymentRecordJSON.isEmpty());
    }

    @Transaction(intent = Transaction.TYPE.SUBMIT)
    public PaymentRecordCO CreatePaymentRecord(final Context ctx, String paymentRecordJson) {
        ChaincodeStub stub = ctx.getStub();
        if (PaymentRecordExists(ctx, paymentRecordJson)) {
            String errorMessage = String.format("PaymentRecord already exists");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }
        PaymentRecordCO paymentRecord = genson.deserialize(paymentRecordJson, PaymentRecordCO.class);
        stub.putStringState(paymentRecord.formKey(), paymentRecordJson);
        return paymentRecord;
    }
    @Transaction(intent = Transaction.TYPE.EVALUATE)
    public PaymentRecordCO ReadPaymentRecord(final Context ctx, String paymentRecordId) {
        ChaincodeStub stub = ctx.getStub();
        String keyPrefiexPaymentRecordId = PaymentRecordCO.formKey(Long.valueOf(paymentRecordId));
        String paymentRecordJSON = stub.getStringState(keyPrefiexPaymentRecordId);
        if (paymentRecordJSON == null || paymentRecordJSON.isEmpty()) {
            String errorMessage = String.format("PaymentRecord does not exist");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }

        PaymentRecordCO paymentRecord = genson.deserialize(paymentRecordJSON, PaymentRecordCO.class);
        return paymentRecord;
    }
    @Transaction(intent = Transaction.TYPE.SUBMIT)
    public PaymentRecordCO UpdatePaymentRecord(final Context ctx, String paymentRecordJson) {
        ChaincodeStub stub = ctx.getStub();
        PaymentRecordCO paymentRecord = genson.deserialize(paymentRecordJson, PaymentRecordCO.class);

        if (!PaymentRecordExists(ctx, paymentRecordJson)) {
            String errorMessage = String.format("PaymentRecord does not exist");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }
        String oldPaymentRecordJson = stub.getStringState(paymentRecord.formKey());
        PaymentRecordCO oldPaymentRecord = genson.deserialize(oldPaymentRecordJson, PaymentRecordCO.class);
        String sortedJson = genson.serialize(paymentRecord);
        stub.putStringState(paymentRecord.formKey(), sortedJson);
        return oldPaymentRecord;
    }
    @Transaction(intent = Transaction.TYPE.SUBMIT)
    public void DeletePaymentRecord(final Context ctx, String paymentRecordId) {
        ChaincodeStub stub = ctx.getStub();

        if (!PaymentRecordExists(ctx, paymentRecordId)) {
            String errorMessage = String.format("PaymentRecord does not exist");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }

        stub.delState(paymentRecordId);
    }
}





